"""
property 装饰器是一种非常有用的功能，它允许你将类的方法转换为属性访问的形式。这使得你可以像访问普通属性一样访问这些方法，
同时还可以在访问、设置和删除这些属性时执行自定义的逻辑。
"""


class Person:
    def __init__(self, name, age):
        self._name = name
        self._age = age

    @property
    def age(self):
        """Getter for age."""
        print("Getting age...")
        return self._age

    @age.setter
    def age(self, value):
        """Setter for age with validation."""
        print("Setting age...")
        if not isinstance(value, int):
            raise ValueError("Age must be an integer.")
        if value < 0:
            raise ValueError("Age cannot be negative.")
        self._age = value

    @age.deleter
    def age(self):
        """Deleter for age."""
        print("Deleting age...")
        del self._age


# 创建一个 Person 对象
person = Person('Alice', 30)

# 访问 age 属性
print(person.age)  # 输出: Getting age... 30

# 设置 age 属性
person.age = 35  # 输出: Setting age...

# 尝试设置一个无效的 age 值
try:
    person.age = -10
except ValueError as e:
    print(e)  # 输出: Age cannot be negative.

# 删除 age 属性
del person.age  # 输出: Deleting age...
